package com.nstar.orderexpress.view
{
	import com.nstar.base.util.EffectUtils;
	import com.nstar.base.util.Logger;
	import com.nstar.orderexpress.control.LoginEvent;
	import com.nstar.orderexpress.control.MainMenuClickEvent;
	import com.nstar.orderexpress.model.Model;
	import com.nstar.orderexpress.mxml.CompareResultView;
	import com.nstar.orderexpress.mxml.FavoriteView;
	import com.nstar.orderexpress.mxml.MainView;
	import com.nstar.orderexpress.mxml.MenuView;
	import com.nstar.orderexpress.mxml.SearchResultView;
	import com.nstar.orderexpress.mxml.SearchView;
	import com.nstar.orderexpress.mxml.ShoppingCartView;
	import com.nstar.orderexpress.mxml.UserView;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.ViewStack;
	import mx.controls.Label;
	import mx.controls.ToggleButtonBar;
	import mx.core.Application;
	import mx.events.FlexEvent;
	import mx.events.ItemClickEvent;

	public class OrderExpressApp extends Application
	{
		public var vsResultView:ViewStack;
		public var vsMain:ViewStack;
		public var vsControl:ViewStack;
		
		public var menuView:MenuView;
		public var mainView:MainView;
		public var searchResultView:SearchResultView;
		public var compareResultView:CompareResultView;
		public var userView:UserView;
		public var searchView:SearchView;
		public var shoppingCartView:ShoppingCartView;
		public var favoriteView:FavoriteView;
		
		public var tbbControl:ToggleButtonBar;
		
		private var model:Model = Model.getInstance();
		private var logger:Logger = Logger.getInstance();
		
		public function OrderExpressApp()
		{
			//TODO: implement function
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
			this.addEventListener(LoginEvent.LOGIN,loginHandler);
		}
		
		private function loginHandler(event:LoginEvent):void{
			userView.imgStatus.source = AppImages.login;
			userView.imgStatus.toolTip = "register logined.";
		}
		
		private function init(event:FlexEvent):void{
			model.mainApp = this;
			menuView.addEventListener(MainMenuClickEvent.MAIN_MENU_CLICK,resizeView);
			tbbControl.addEventListener(ItemClickEvent.ITEM_CLICK,itemclickHandler);
			addMenuChild(null);
		}
		
		private function itemclickHandler(event:ItemClickEvent):void{
			EffectUtils.rotateViewStack(vsControl);
		}
		
		//调整主视图和搜索/比较结果视图的大小
		//如果event是null，表示是手工调用的该方法
		public function resizeView(event:MainMenuClickEvent):void{
			if (event == null){
				//如果搜索结果已经显示，则不再改变视图大小
				if (vsResultView.height>0) return;
				
				var oldHeight:int = vsMain.height;
				vsMain.height = 0;
				
				vsResultView.y = vsMain.y;
				vsResultView.height = oldHeight;
			}else{
				//如果主视图已经显示，则不再改变视图大小
				if (vsMain.height>0) return;
				
				var oldHeight:int = vsResultView.height;
				vsResultView.height = 0;
				
				vsMain.height = oldHeight;				
			}
			
		}
		
		private function addMenuChild(event:*):void{
			var tmp:ArrayCollection = model.mainApp.menuView.bbMenus.dataProvider as ArrayCollection;
			for(var i:int = 0;i < tmp.length;i++){
				if (i == 0 ) continue;//第一项跳过去
				var cav:Canvas = new Canvas();
				var label:Label = new Label();
				label.text = "load "+tmp[i];
				cav.addChild(label);
				cav.label = tmp[i];
				vsMain.addChild(cav);
			}
			menuView.bbMenus.dataProvider = vsMain;
			
		}
	}
}